/**
 * @since 8 may 2012
 * @version 0.7
 * @author Koen Ursem
 */
package config;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Properties;

/**
 * @author Koen Ursem The Config class for loading and saving configurations
 * needed by the program
 */
public class Config {

    //config.properties values that are saved into all config objects
    public static String ip;
    public static int port;
    //creation of a properties class for the config
    private static Properties config = new Properties();
	public static String queryStatus = null;

    /**
     * Constructor for the config class
     *
     * @author Koen Ursem
     */
    public Config() {
	//empty loader for starting up statics
    }

    /**
     * Load of the configs from the basic config.properties file
     *
     * @author Koen Ursem
     */
    public void loadConfigs() {
	//user directory where the application is running from
	String dir = System.getProperty("user.dir");

	try {
	    //load the data from the file ! TODO: fix links for the final build version
	    config.load(new FileInputStream(dir + "/config.properties"));
	} //Catch for when the config file was not found
	catch (FileNotFoundException fnfe) {
	    Content.log.println("The config.properties file was not found, loading defaults.");
	} //catch for when file is  not allowed to be read
	catch (SecurityException se) {
	    Content.log.println("The file could not be read, try to run this program as administrator.");
	} //catch for other errors
	catch (IOException ioe) {
	}

	//save the config values into the static publics 2nd value is a backup value when no config was found
	ip = config.getProperty("serverip", "127.0.0.1");

	//try to catch error if port is no integer and reset to default
	try {
	    port = Integer.parseInt(config.getProperty("poort", "1025"));
	} catch (NumberFormatException nfe) {
	    Content.log.println("The port is configured wrong! It has been set to default: 1025.");
	    config.setProperty("poort", "1025");
	    port = 1025;
	}
	Content.log.println("Configs are (re)loaded.");
    }

    /**
     * Loading of the configs from a different named config file
     *
     * @param name = the name of the config file that is needed to be read
     * @author Koen Ursem
     */
    public void loadConfigsName(String name) {

	//user directory where the application is running from
	String dir = System.getProperty("user.dir");

	try {
	    //Load the configs from the  file 
	    config.load(new FileInputStream(dir + "/" + name));
	} //Catch for when the config file was not found
	catch (FileNotFoundException fnfe) {
	    Content.log.println("The config.properties file has not been found, loading defaults.");
	} //catch for when file is  not allowed to be read
	catch (SecurityException se) {
	    Content.log.println("The file could not be read, try to run this program as administrator.");
	} //catch for other errors
	catch (IOException ioe) {
	}

	//Save the config values into the public statics
	ip = config.getProperty("serverip", "127.0.0.1");
	//try to catch error if port is no integer and reset to default
	try {
	    port = Integer.parseInt(config.getProperty("poort", "1025"));
	} catch (NumberFormatException nfe) {
	    Content.log.println("The port is configured wrong! It has been set to default: 1025.");
	    config.setProperty("poort", "1025");
	    //port = 1025;
	}
	Content.log.println("Configs have been (re)loaded.");
    }

    /**
     * Update a existing attribute or add a new attribute to the configs
     *
     * @param attribute = the name of the attribute attributeValue = the value of the attribute
     * @author Koen Ursem
     */
    public void updateConfig(String attribute, String attributeValue) {
	//val1 = the attribute name, attributeValue = the attribute value
	config.setProperty(attribute, attributeValue);
    }

    /**
     * Save the configs into the basic config.properties file
     *
     * @author Koen Ursem
     */
    public void saveConfigs() {
	//user directory war de applicatie gestart is
	String dir = System.getProperty("user.dir");

	try {
	    //Saving the configs into the config file
	    config.store(new FileOutputStream(dir + "/config.properties"), null);
	} catch (FileNotFoundException fnfe) {
	    Content.log.println("The configs have not been saved, due to an incorrect name or folder given.");
	} //catch for when file is  not allowed to change
	catch (SecurityException se) {
	    Content.log.println("The file could not be created, try to run this program as administrator.");
	} //catch for other errors
	catch (IOException ex) {
	}
	Content.log.println("Configs have been saved.");
    }

    /**
     * Save the configs into a configs file with a different name
     *
     * @param name = the name for the new config file
     * @author Koen Ursem
     */
    public void saveConfigsName(String name) {
	//user directory where the application is running in
	String dir = System.getProperty("user.dir");

	try {
	    //Saving the configs into the config file
	    config.store(new FileOutputStream(dir + "/" + name), null);
	} catch (FileNotFoundException fnfe) {
	    Content.log.println("The configs have not been saved, due to an incorrect name or folder given.");
	} //catch for when file is  not allowed to change
	catch (SecurityException se) {
	    Content.log.println("The file could not be created, try to run this program as administrator.");
	} //catch for other errors
	catch (IOException ex) {
	}
	Content.log.println("Configs have been saved.");
    }

    /**
     * Loading of a single attribute from the loaded configs (for attributes
     * that are not made public static)
     *
     * @param attributeName = the name of the attribute that you want the values from
     * @author Koen Ursem
     */
    public String getConfig(String attributeName) {
	//stuurd een waarde van het atribuut attributeName terug uit de config file
	return config.getProperty(attributeName);
    }

    /**
     * Creating a backup of the configs file using the current date and time
     * down to seconds to save it as
     *
     * @author Koen Ursem
     */
    public void backupConfigs() {
	//creating a backup of the loaded configs with the current date
	DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
	Calendar cal = Calendar.getInstance();
	String date = (dateFormat.format(cal.getTime()));
	//to save the configs into a file
	Content.log.println("Creating backup.");
	this.saveConfigsName(date + "_backup.properties");
    }
}
